<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    body, div {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .blue {
      width: 300px;
      height: 300px;
      background-color: blue;
    }
    .green {
      width: 200px;
      height: 200px;
      background-color: green;
    }
    .red {
      width: 100px;
      height: 100px;
      background-color: red;
    }
  </style>
</head>
<body>
  <div class="blue">
    <div class="green">
      <div class="red"></div>
    </div>
  </div>
</body>
</html>

<script>
const domRed = document.querySelector('.red');
const domGreen = document.querySelector('.green');
const domBlue = document.querySelector('.blue');

// 事件传递机制：
  // 由里向外 ->  冒泡 bubble
  // 由外向里 ->  捕获 capture

// 如何将事件由 默认的 冒泡 改为 捕获：
  // addEventListener 第三个参数，设为 ture

domRed.addEventListener('click', e => {
  console.log('red');

  // cancelBubble 和 stopPropagation  亦可作为 取消 事件捕获。
  // 浏览器的兼容性问题：
  // e.cancelBubble = true;
  // e.stopPropagation(); // 兼容性写法
}, true);


domGreen.addEventListener('click', e => {
  console.log('green');
}, true);

domBlue.addEventListener('click', e => {
  // e.cancelBubble = true;
  console.log('blue');
}, true);

document.body.addEventListener('click', e => {
  console.log('body');
  e.cancelBubble = true;
}, true);

document.addEventListener('click', e => {
  console.log('document');
  // e.cancelBubble = true;
  // e.stopPropagation(); // 兼容性写法
}, true);

window.addEventListener('click', e => {
  console.log('window');
}, true);
</script>